from django.shortcuts import render, HttpResponse, redirect
from django.views.decorators.csrf import csrf_exempt
from django import forms
from django.core.validators import RegexValidator

# Create your views here.


@csrf_exempt
def ab_form(request):
    back_dic = {'username': '', 'password': ''}
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        if '金瓶梅' in username:
            back_dic['username'] = '不符合社会主义核心价值观'
        if len(password) < 3:
            back_dic['password'] = '不能太短 不好!'
    return render(request, 'ab_form.html', locals())


class MyForm(forms.Form):
    username = forms.CharField(min_length=3, max_length=8, label='用户名',
                               error_messages={
                                   'min_length': '用户名最少3位',
                                   'max_length': '用户名最长8位',
                                   'required': '用户名不能为空'
                               }, initial='AK-DSB', widget=forms.widgets.TextInput(attrs={'class': 'form-control'})
                               )
    password = forms.CharField(min_length=3, max_length=8,error_messages={
                                   'min_length': '密码最少3位',
                                   'max_length': '密码最长8位',
                                   'required': '密码不能为空'
                               }, widget=forms.widgets.PasswordInput(attrs={'class': 'form-control'}))
    confirm_password = forms.CharField(min_length=3, max_length=8,
                                       error_messages={
                                           'min_length': '确认密码最少3位',
                                           'max_length': '确认密码最长8位',
                                           'required': '请确认密码'
                                       }, widget=forms.widgets.PasswordInput(attrs={'class': 'form-control c c1'}))
    email = forms.EmailField(label='邮箱',
                             error_messages={
                                'invalid': '邮箱格式不正确',
                                'required': '邮箱不能为空', }, widget=forms.widgets.EmailInput(attrs={'class': 'form-control'}))

    phone = forms.CharField(
        label='手机号',
        initial=18573778821,
        validators=[RegexValidator(r'^[0-9]+$', '请输入数字'), RegexValidator(r'^15789[0-9]+$', '请输入正确的手机号')],
    )

    def clean_username(self):
        username = self.cleaned_data.get('username')

        if '666' in username:
            self.add_error('username', '光喊666是不行的')
        return username

    def clean(self):
        password = self.cleaned_data.get('password')
        confirm_password = self.cleaned_data.get('confirm_password')
        if not confirm_password == password:
            self.add_error('confirm_password', '两次密码不一致')
        return self.cleaned_data


@csrf_exempt
def index(request):
    form_obj = MyForm()
    print('无request的form:', form_obj)
    if request.method == 'POST':
        # 校验数据
        form_obj = MyForm(request.POST)
        print('有request的form:', form_obj)
        print('fields:', form_obj.fields)
        # 判断数据是否合法
        if form_obj.is_valid():
            print(form_obj.cleaned_data)
            return HttpResponse('OK')
        else:
            pass
    return render(request, 'index.html', locals())